﻿using System.Collections.Generic;
using UnityEngine;
using FairyGUI;
using System;
using System.Collections;

public class UIControl
{
    // Start is called before the first frame update
    //private GComponent view;
    private static UIControl inst;
    private List<GComponent> combs = new List<GComponent>();
    private List<GComponent> states = new List<GComponent>();
    private DragonBones.Animation dragonAn;
    private int score { get; set; }
    private int Comb { get; set; }
    //private Transition tarnCom;
    //private Transition tarnState;
    public static UIControl instance
    {
        get
        {
            if (inst == null)
                inst = new UIControl();

            return inst;
        }
    }
    public void Create()
    {
        //UIPackage.AddPackage("Fairy/imcui");
    }

    // Update is called once per frame

    public void playComB() {
        clearComList();
        GComponent view = UIPackage.CreateObject("imcui", "uicomp").asCom;
        GRoot.inst.AddChild(view);
        GGroup ggp = view.GetChild("combc").asGroup;
        GTextField combText = view.GetChild("comb").asTextField;
        Transition tarnCom = view.GetTransition("combani");
        combs.Add(view);
        ggp.visible = true;
        combText.text = Comb.ToString(); 
        tarnCom.Play(() => {
            view.Dispose();
        });
    }
    public void playState(bool state) {
        clearStaList();
        GComponent view = UIPackage.CreateObject("imcui", "uicomp").asCom;
        GRoot.inst.AddChild(view);
        GLoader aLoader = view.GetChild("n11").asLoader;
        Transition tarnState = view.GetTransition("cinfoani");
        states.Add(view);
        aLoader.visible = false;
        if (!state)
        {
            aLoader.url = "ui://dlyeomw3eqe26";
            Comb = 0;
        }
        else {
            Comb++;
        }
        if (Comb>=2) {
            playComB();
        }
        tarnState.Play(() => {
            view.Dispose();
        });
    }
    public void palyDragonAni(string anim)
    {
        dragonAn.Play(anim);
    }
    public void clearScore()
    {
        this.score = 0;
    }
    void clearComList() {
        for (int i = 0; i < combs.Count; i++) {
            if (combs[i] !=null) {
                combs[i].Dispose();
            }
        }
    }
    void clearStaList() {
        for (int i = 0; i < states.Count; i++)
        {
            if (states[i] != null)
            {
                states[i].Dispose();
            }
        }
    } 
}
